package com.zl.smartmall.library.os;

import com.loopj.android.http.RequestParams;
import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.listener.CheckVersionListener;
import com.zl.smartmall.library.listener.DownloadNewAppListener;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;
import com.zl.smartmall.library.util.Tools;

import android.content.Context;

/**
 * 系统管理类
 * @author 陈希然
 *
 */
public class System {

	private static final String TAG = System.class.getSimpleName();

	/**
	 * 检查程序版本号
	 * @param context 上下文对象
	 * @param packageSuffix 包名最后一段
	 * @param checkVersionListener 回调监听器
	 */
	public static void checkVersion(Context context, String packageSuffix, CheckVersionListener checkVersionListener) {
		if(!Tools.hasInternetConnection(context)) {
			checkVersionListener.onNoNetWork();
		
		}else {
			String versionName = Tools.getAppVersionName(context);
			RequestParams params = new RequestParams();
			params.put("channel", packageSuffix);
			params.put("appver", versionName);
//			params.put("packname", mContext.getPackageName());
			checkVersionListener.versionName = versionName;
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(Constants.URL_CHECK_VERSION, params, checkVersionListener);
			CrashHandler.d(TAG, "url = " + Constants.URL_CHECK_VERSION + "?" + params);
		}
	}
	
	/**
	 * 下载新版app
	 * @param context 上下文对象
	 * @param url 下载url
	 * @param downloadNewAppListener 回调监听器
	 */
	public static void downloadVersion(Context context, String url, DownloadNewAppListener downloadNewAppListener) {
		if(!Tools.hasInternetConnection(context)) {
			downloadNewAppListener.onNoNetWork();
		
		}else {
			SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
			SingtonAsyncHttpClient.getInstance().get(url, downloadNewAppListener);
			CrashHandler.d(TAG, "url = " + url);
		}
	}
}
